package cn.wen.core.ui;

import android.os.Build;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.wen.base.base.BaseActivity;
import cn.wen.base.image.ImageLoader;
import cn.wen.core.R;
import cn.wen.core.eventbus.EventBus;
import cn.wen.core.titlebar.DefBarBuild;
import cn.wen.core.titlebar.DefTitleBar;
import cn.wen.core.titlebar.TitleBarUtil;
import cn.wen.core.ui.loading.LoadingManager;
import cn.wen.core.ui.loading.OnLoadingListener;


/**
 * AppActivity 应用的Activity
 */

public abstract class AppActivity extends BaseActivity {
    /**
     * 自定义标题
     */
    protected DefTitleBar mTitleBar;
    /**
     * 加载页面
     */
    protected LoadingManager mLoadingLayout;
    /**
     * ButterKnife注解
     */
    private Unbinder mBinder;

    @Override
    protected final void initTitle() {

        if (isNeedTitle()) {
            mTitleBar = new DefBarBuild(this, getTitleParent())
                    .setLeftRes(R.drawable.ic_title_back)
                    .setBgColor(getResources().getColor(R.color.colorPrimary))
                    .build();
            initTitle(mTitleBar);
            // 兼容 4.4 状态栏 && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                TitleBarUtil.setStatusBarColor(this);
                TitleBarUtil.setTitlePadding(this, mTitleBar.getTitleBar());
            }
        }

    }

    /**
     * 加载布局作用在哪个View上
     */
    protected Object getLoadingParentView() {
        return mContentView;
    }

    private class AppLoadingListener extends OnLoadingListener {

        @Override
        public void setRetryEvent(View retryView) {
            setViewClick(retryView);
        }

        @Override
        public void setDataErrorEvent(View errorDataView) {
            setViewClick(errorDataView);
        }

        @Override
        public int generateEmptyLayoutId() {
            int emptyLayoutId = getEmptyLayoutId();
            if (emptyLayoutId != -1) {
                return emptyLayoutId;
            }
            return super.generateEmptyLayoutId();
        }

        @Override
        public void setEmptyEvent(View emptyView) {
            setEmptyEventClick(emptyView);
        }
    }

    /**
     * 默认加载页面的监听
     */
    protected OnLoadingListener getOnLoadingListener() {
        return new AppLoadingListener();
    }

    /**
     * 网络失败点击重新调用
     */
    protected void onRetryClick() {
        initNet();
    }

    /**
     * 空页面处理事件
     *
     * @param emptyView
     */
    protected void setEmptyEventClick(View emptyView) {
    }

    /**
     * 空布局
     */
    protected int getEmptyLayoutId() {
        return -1;
    }

    /**
     * 设置错误View的点击
     *
     * @param retryView
     */
    private void setViewClick(View retryView) {
        if (retryView != null) {
            retryView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRetryClick();
                }
            });
        }
    }


    /**
     * 初始化设置Title
     */
    protected abstract void initTitle(DefTitleBar titleBar);

    /**
     * Title作用在哪个View上
     */
    protected ViewGroup getTitleParent() {
        return null;
    }

    /**
     * 是否有Title，默认是有的
     *
     * @return
     */
    protected boolean isNeedTitle() {
        return true;
    }

    @Override
    protected final void bindView(View contentView) {
        mBinder = ButterKnife.bind(this, contentView);
        EventBus.getDefault().register(this);
        // 初始化加载页面
        mLoadingLayout = LoadingManager.create(getLoadingParentView(), getOnLoadingListener());
        mLoadingLayout.showContent();
    }

    @Override
    protected final void unbindView(View contentView) {
        EventBus.getDefault().unregister(this);
        if (mBinder != null) {
            mBinder.unbind();
            mBinder = null;
        }
        ImageLoader.clear(this);
        mLoadingLayout = null;
        mTitleBar = null;
    }


}
